package com.hellobike.haxing.modules.wms.utils;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class HtmlToPdf {
    public static String getCurrentOperatingSystem() {
        String os = System.getProperty("os.name").toLowerCase();
        System.out.println("---------当前操作系统是--------   ---" + os);
        return os;
    }

    public static void topdf(String content, String path) throws DocumentException, FileNotFoundException {
        ITextRenderer renderer = new ITextRenderer();
        ITextFontResolver fontResolver = renderer.getFontResolver();
        try {
            String name = getCurrentOperatingSystem();
            if (name.indexOf("windows") < 0) {
                fontResolver.addFont("/usr/share/fonts/chinese/simsun.ttc",
                        BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            } else {
                fontResolver.addFont("d:\\simsun.ttc",
                        BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        renderer.setDocumentFromString(content);
        renderer.layout();
        renderer.createPDF(new FileOutputStream(new File(path)));
    }
}
